trigger AttachCampaign_LeadOnUpload on Lead (after insert) {
    
    Campaign cam;
    CampaignMember cm;
    List<Schema.pickListEntry> listPLE = CampaignMember.Status.getDescribe().getPicklistValues();
    String strDefaultValue = '';
    Map<String, String> mapPLE = new Map<String, String>();
    for (Schema.pickListEntry pe : listPLE){
        if (pe.isActive()){
            mapPLE.put(pe.getLabel(), pe.getValue());
            if (pe.isDefaultValue()){
                strDefaultValue = pe.getValue();
            }
        }
    }
    //get campaign values
    Map<Id,Campaign> tempMap = new Map<Id,Campaign>([select id,cnid__C from campaign]);
    //create map for comparison
    Map<String,String> campaignMap = new Map<String,String>{};
    //fill map    
    for(Campaign cp: tempMap.values()){
        campaignMap.put(cp.cnid__C,cp.id);
    }
    
    for (Lead leadRecord: System.Trigger.new) 
    {     
        System.debug('Here inside Campaign');
        System.debug('Here inside' + leadRecord.id);      
        System.debug('Here inside' + leadRecord.Campaign_Text__c);      
        if(leadRecord.Campaign_Text__c!=null)
        {
            System.debug('Here inside Campaign_Text__c');   
            if((leadRecord.Campaign_Member_Status_Text__c!=null)&&(campaignMap.containsKey(leadRecord.Campaign_Text__c)))
            {   
                System.debug('Here inside Campaign_Member_Status_Text__c');   
                String strStatus = mapPLE.get(leadRecord.Campaign_Member_Status_Text__c);
                if (strStatus == null || strStatus == ''){
                    strStatus = strDefaultValue;
                }
                cm = new CampaignMember(LeadId=leadRecord.Id,CampaignId=campaignMap.get(leadRecord.Campaign_Text__c),Status=strStatus);
                if(cm!=null){   
                    insert cm;
                }
            }
        }
    }
}